package com.luoye.lj.platform.dao.impl;


import com.luoye.lj.platform.dao.IRoleServiceDao;
import com.luoye.lj.platform.dao.mapper.IRoleServiceMapper;
import com.luoye.lj.platform.model.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wei on 2015/12/1.
 */

@Repository
public class RoleServiceDaoImpl implements IRoleServiceDao {
    @Autowired
    private IRoleServiceMapper mapper;

    @Override
    public boolean addRole(RoleInfo role)throws RuntimeException{
        /*RoleInfo ro = new RoleInfo(role.getId(),role.getDescription(),role.getRolename());
        RoleInfo roleInfo = mapper.getRoleById(role.getId());
        if (roleInfo == null) {
            mapper.addRole(ro);
            role.setId(ro.getId());
            mapper.addRra(role);
            return true;
        } else if (mapper.getDelmark(roleInfo.getId()) == 1) {
            mapper.recover(role.getId());
            return true;
        } else {
            throw ServiceExceptionConstants.SAVE_FAILED;
        }*/
        if(mapper.addRole(role) > 0){
            return true;
        }
        return false;

    }

    @Override
    public boolean delRole(long id) {
        if(mapper.delRole(id) > 0){
            return true;
        }
        return false;
    }
    @Override
    @Transactional
    public boolean modifyRole(RoleSaveInfo role) {
        mapper.modifyRole(role);
        mapper.delRra(role.getId());
        mapper.addRra(role);
        return true;
       // throw new RuntimeException("");

//        if(mapper.modifyRole(role) > 0){
//            if(mapper.delRra(role.getId()) > 0){
//                mapper.addRra(role);
//            }
           // throw new RuntimeException();
            /*

            List<AuthInfo> auths = role.getAuths();
            List<Long> aids= mapper.getAid(role.getId());
            List<AuthInfo> authInfos = new ArrayList<AuthInfo>();
            List<AuthInfo> list = new ArrayList<AuthInfo>();
            List<AuthInfo> _list = new ArrayList<AuthInfo>();
            for (Long aid:aids){
                AuthInfo auth = mapper.getAuth(aid);
                authInfos.add(auth);
            }
            for (int i=0;i<auths.size();i++ ){
                boolean flag = false;
                for (int j=0;j<authInfos.size();j++){
                    if(auths.get(i).getId() == authInfos.get(j).getId()){
                        flag = true;
                    }
                }
                if(flag){
                    list.add(auths.get(i));
                }else{
                    _list.add(auths.get(i));
                }
            }
            if(list.size() > auths.size()){

            }
            for (AuthInfo auth:_list){
                mapper.addRra();
            }*/

//        }
//        return false;
    }
    @Override
    public List<RoleInfo> getAll() {
        return mapper.getAll();
    }

    @Override
    public RoleCollection search(RoleInfoSearch search) {
        List<RoleInfo> roleInfos = mapper.search(search);
        List<RoleSaveInfo> roleSaveInfos = new ArrayList<>();
        try {
            for (RoleInfo role : roleInfos) {
                Long rid = role.getId();
                List<Long> aids = mapper.getAid(rid);
                RoleSaveInfo roleSaveInfo = new RoleSaveInfo(role);
                roleSaveInfo.setAuths(new ArrayList<AuthInfo>());
                for (long aid : aids) {
                    AuthInfo auth = mapper.getAuth(aid);
                    roleSaveInfo.getAuths().add(auth);
                }
                roleSaveInfos.add(roleSaveInfo);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        RoleCollection roleDetail = new RoleCollection();
        roleDetail.setRoles(roleSaveInfos);
        roleDetail.setTotalCount(mapper.talCount(search));
        return roleDetail;
    }

    @Override
    public boolean addRra(RoleSaveInfo rolesave) {
        if(mapper.addRra(rolesave) > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delRra(long rid, long aid) {
        if(mapper.delRra(rid) > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean recover(long id) {
        if(mapper.recover(id) > 0){
            return true;
        }
        return false;
    }

    @Override
    public int getDelmark(long id) {
        return mapper.getDelmark(id);
    }

    @Override
    public RoleInfo getRoleById(long id) {
        return mapper.getRoleById(id);
    }
    public RoleInfo getRoleByname(String description){
        return mapper.getRoleByname(description);
    }
}
